﻿Imports System.Data.SqlClient

Public Class Frm_Idafa_Chohoud
    Public dv As Dossier
    Public cli As Temoins
    Public TemDoss As Dossier_temoins
    Dim deChod As DictionaryEntry
    Dim checkHand As Boolean = True
    'Remplir le comboBox par le nom et le prenom avec un id
    Public Sub remplirTemoins()
        Partager.moi.remplir_List_temoins()

        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each clt As Temoins In Partager.moi.List_temoins
            str = clt.Nom & "  " & clt.Prenom
            htClient.Add(clt.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            Comb_chahid.Items.Add(ImageType)
        Next
        Comb_chahid.DisplayMember = "value"
        Comb_chahid.ValueMember = "key"
        If Comb_chahid.Items.Count > 0 Then
            Comb_chahid.SelectedIndex = 0
        End If

    End Sub
    'Remplir le comboBox par le nom et le prenom avec un id
   

    Public Sub Remplir_ListTout()
        List_tous.Items.Clear()
        For Each khib As Dossier_temoins In dv.List_temoins
            For Each clt As Temoins In Partager.moi.List_temoins
                If khib.Id_temoins = clt.Id Then
                    List_tous.Items.Add(New ListViewItem(New String() {clt.Cin, clt.Nom, clt.Prenom}))
                End If
            Next
        Next
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        If Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            If Comb_chahid.Text <> "" Then

                Try
                    If MsgBox("هل تريد فعلا إضافة هذا الشاهد لهذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                        Partager.close()
                        deChod = DirectCast(Comb_chahid.SelectedItem, DictionaryEntry)
                        Dim idchahid As Integer = CInt(deChod.Key)


                        TemDoss = New Dossier_temoins(dv.id, idchahid)
                        dv.Ajouter_temoins(TemDoss)

                        Remplir_ListTout()

                        MsgBox("تمت إضافة الشاهد للملف بنجاح", MsgBoxStyle.Information, "نجاح")
                    End If
                Catch sqlEx As SqlException
                    If sqlEx.Message.Contains("PK_Dossier_temoins") Then
                        MsgBox("عفوا، هذا الشاهد تمت إضافته لهذاالملف من قبل", MsgBoxStyle.Critical, "خطأ")
                    Else
                        MsgBox(sqlEx.Message)
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الشاهد من هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim teDoss As Dossier_temoins = dv.List_temoins(List_tous.SelectedIndices.Item(0))
                    dv.suprimmer_temoins(teDoss)

                    Remplir_ListTout()

                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر شاهد من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub Frm_Idafa_Chohoud_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()
        Partager.nonInitial = New ArrayList
        Partager.nonInitial.Add("Comb_marji3ona")
        Partager.remplirDossier(Comb_marji3ona)
        '----------------------------------------------------------------
        remplirTemoins()
        '----------------------------------------------------------------
    End Sub

   

  

    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_temoins()
                    Exit For
                End If
            Next
            Partager.moi.remplir_List_temoins()
            Remplir_ListTout()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
           Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub

    Private Sub btn_rech_chahid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_rech_chahid.Click
        Me.Enabled = False
        Frm_Rech_chohoud.Show()
        Partager.type = "Ch_Frm_Idafa_Chohoud"
    End Sub
End Class